package com.link.base.base.address.service;

import com.alibaba.fastjson.JSON;
import com.link.base.base.address.dao.mybatis.mapper.AllAddressMapper;
import com.link.base.base.address.model.AddrTree;
import com.link.base.base.address.model.AllAddress;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.RedisUtil;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 10:27
 * @version 1.0
 */

@Service
public class AllAddressServiceImpl extends BasicServiceImpl<AllAddress> implements AllAddressService {
    @Resource
    private AllAddressMapper allAddressMapper;

    @Override
    public BasicMapper<AllAddress> getBasicMapper() throws Exception {
        return allAddressMapper;
    }

    /**
     * 更新前操作
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforUpdate(AllAddress entity) throws Exception {
        //县
        String county = "County";
        try {
            if (county.equals(entity.getAddrTypes())) {
                allAddressMapper.allByCountryUpdate(entity);
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
    }

    /**
     * 修改父地址信息
     * @param record
     * @throws BasicServiceException
     */
    @Override
    public void parentIdUpdate(AllAddress record) throws BasicServiceException {
        try {
            allAddressMapper.parentIdUpdate(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
    }

    /**
     * 根据通用类型得到所有的省地址信息
     * @param record
     * @return List
     * @throws Exception
     */
    @Override
    public List<AllAddress> queryByTypeAddressListPage(AllAddress record) throws Exception {
        List<AllAddress> list = null;
        try {
            list = allAddressMapper.queryByTypeAddressListPage(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
        return list;
    }

    /**
     * 查询全部
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public List<AllAddress> queryAll(AllAddress entity) throws Exception {
        LCLogger.info().withMessageKey("queryAll(AllAddress entity)").withMessage("查询地址信息.").flush();
        LinkRedisClient j = RedisUtil.getJedis();
        String redisKey = "app_lov_address";
        List<AllAddress> list = new ArrayList<>();
        try {
            if (j.exists(redisKey)) {
                list = JSON.parseArray(j.get(redisKey), AllAddress.class);
            } else {
                AllAddress address = new AllAddress();
                list = super.queryAll(address);
                j.set(redisKey, JSON.toJSONString(list));
                // 10分钟
                j.expire(redisKey, 60 * 10 * 1);
            }
        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(j);
        }

        return list;
    }

    /**
     * 查询树形结构省市县
     *
     * @param record
     * @throws Exception
     */
    public List<AddrTree> queryAllAddress(AddrTree record) throws Exception {
        return allAddressMapper.queryAllAddress(record);
    }
}
